<template>
	<view :class="['main_style','main_bg']">
		<block v-for="(item,index) in list" :key="index">
			<button class="btn_class" v-if="item.type == 'share'" open-type="share">
				<image class="left_classz" :src="cacheCmage('wechat.png')"></image>
				<view class="middle_class">
					<view class="middle_top">{{item.title}}</view>
					<view class="middle_bottom">{{item.subTitle}}</view>
				</view>
			</button>
			<view class="item_class" @click="getQr(item)" v-else>
				<image class="left_class" :src="item.image"></image>
				<view class="middle_class">
					<view class="middle_top">{{item.title}}</view>
					<view class="middle_bottom">{{item.subTitle}}</view>
				</view>
			</view>
		</block>
		<ad unit-id="adunit-d65baccbcd5f3c71" class="adunit_class"></ad>
		<!-- <view style="position: fixed; background-color: red;z-index: 99;width: 100%;height: 200rpx;bottom: 0rpx;pointer-events: none;" ></view> -->
		<u-modal v-model="show" :content="content" @confirm="confirm"></u-modal>
		
		<u-popup ref="share" mode="center" border-radius="10">
			<shareView :callback="shareCallback" title="每日分享一次才能获取群二维码"></shareView>
		</u-popup>
	</view>
</template>

<script>
	var _this = this;
	var videoAd = null;
	import dateTool from '@/common/utils/dateTool.js';
	import shareView from "@/pages/homeList/share/main.vue";
	export default {
		components:{shareView},
		data() {
			return {
				advModel:null,
				selectItem:null,
				content:'二维码会自动保存到您到相册,请记得授权哦',
				show:false,
				list:[{
					'image':'',
					'title':'分享',
					'subTitle':'分享给好友或朋友圈,让大家一起帮你砍价',
					'type':'share',
					"image":""
				},{
					'image':'',
					'title':'互砍群',
					'subTitle':'每日及时更新互砍群入口',
					'type':'pddGroup',
					"image":"https://wx.qlogo.cn/mmhead/Q3auHgzwzM6PEheSrqVtmQdJ4yeekicukKB3Vkp5rw56vRg4D7S3brQ/0"
				},{
					'image':'',
					'title':'外卖群',
					'subTitle':'每日及时更新外卖红包群入口',
					'type':'wmRedGroup',
					"image":"https://bkimg.cdn.bcebos.com/pic/8435e5dde71190ef76c65edbb6518a16fdfaaf51cad6?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UyNzI=,g_7,xp_5,yp_5"
				},
				{
					'image':'',
					'title':'小福利',
					'subTitle':'更多福利平台',
					'type':'platform',
					"image":"https://www.fangdong.host/res/redpackage/gulia.png"
				},
				]
			}
		},
		onLoad() {
			_this = this;
			_this.loadAdv();
			_this.loadInterstitialAd();
		},
		onShareAppMessage(options) {
			return this.$shareUtils.share({
				options,
				title: '每天实时更新互砍互助群'
			});
		},
		methods: {
			cccccc(){
				console.log('cccccc');
			},
			getAdv(item) {
				
				this.$request.post({
					url: "/xxADVadv/adv",
					data: {
						user_random: ''
					},
					needEncryption: true,
					success: (res) => {
						console.log(res);
						this.advModel = res;
						this.playAdv();
					},
					fail(failed) {
						if (failed.code == 402) {
							uni.showToast({
								title: failed.message,
								icon: 'none',
								duration: 2000
							})
						}
					}
				});
			},
			loadInterstitialAd(){
				// 在页面中定义插屏广告
				let interstitialAd = null;
				// 在页面onLoad回调事件中创建插屏广告实例
				if (wx.createInterstitialAd) {
				
					interstitialAd = wx.createInterstitialAd({
						adUnitId: this.$config.interstitialAdUnitId
					})
					interstitialAd.onLoad(() => {})
					interstitialAd.onError((err) => {})
					interstitialAd.onClose(() => {})
				}
				
				// 在适合的场景显示插屏广告
				if (interstitialAd) {
					interstitialAd.show().catch((err) => {
						console.error(err)
					})
				}
			},
			loadAdv() {
				if (wx.createRewardedVideoAd) {
					videoAd = wx.createRewardedVideoAd({
						adUnitId: this.$config.rewardedAdUnitId
					})
					videoAd.onLoad(() => {
						console.log('onLoad event emit')
					})
					videoAd.onError((err) => {
						console.log('onError event emit', err)
					})
					videoAd.onClose((res) => {
						console.log('onClose event emit', res)
						// 用户点击了【关闭广告】按钮
						if (res && res.isEnded) {
							// 正常播放结束，可以下发游戏奖励
							this.fetchQr(this.selectItem);
							// this.$refs.advListRef.finishAdv();
						} else {
							// 播放中途退出，不下发游戏奖励
							uni.showToast({
								title: '观看广告无效,不给予奖励.如多次失败,请尝试重新页面.',
								icon: 'none',
								duration: 2000
							})
						}
					})
				}
			},
			saveImageToPhotosAlbum(e) {
				uni.saveImageToPhotosAlbum({
					filePath: e,
					success: function() {
						uni.showToast({
							title: '保存成功,快去扫描二维码吧！',
							icon: 'none',
							duration: 2000
						})
					},
					fail() {
						uni.showToast({
							title: '取消保存',
							icon: 'none',
							duration: 2000
						})
					}
				});
			},
			cache_image(url) {
				var local_image = this.$request.cache_image({
					image_url: url,
					call_back:(data)=> {
						this.saveImageToPhotosAlbum(data);
					}
				})
				if (this.$u.test.isEmpty(local_image)) {
					return this.$config.image_host + url;
				}
				return local_image;
			},
			cacheCmage(url) {
				var local_image = this.$request.cache_image({
					image_url: this.$config.image_host + url,
					call_back(data) {
						_this.reload = data;
					}
				})
				if (this.$u.test.isEmpty(local_image)) {
					return this.$config.image_host + url;
				}
				return local_image;
			},
			fetchQr(item){
				uni.showLoading({});
				
				this.$request.post({
					url: "/mQr/qrDetail",
					data:{qr_type:item.type},
					needEncryption: true,
					success:(res)=> {
						uni.hideLoading();
						console.log(res);
						if(res != null){
							let u = this.cache_image(res.qr_url);
							if(u.length > 0)this.saveImageToPhotosAlbum(u);
						}else{
							uni.showToast({
								duration:2000,
								title:"获取失败,请重新获取",
								icon:'none'
							})
						}
					},
					fail(failed) {
						uni.hideLoading();
					}
				});
			},
			playAdv() {
				console.log("videoAd");
				if (videoAd) {
					videoAd.show().catch(() => {
						// 失败重试
						videoAd.load()
							.then(() => videoAd.show())
							.catch(err => {
								console.log('激励视频 广告显示失败')
							})
					})
				} else {
					console.log('playfailed');
				}
			},
			confirm(){
				this.fetchQr(this.selectItem);
			},
			shareCallback(p){
				_this.$refs.share.close();
				if(p == 1){
					uni.setStorageSync(dateTool.getTimeFromat('')+"share","1");
				}
			},
			getQr(item){
				if(item.type == 'pddGroup' || item.type == 'wmRedGroup'){
					let isShare = uni.getStorageSync(dateTool.getTimeFromat('')+"share");
					if(this.$u.test.empty(isShare)){
						this.$refs.share.open();
						return ;
					}
					let alert = uni.getStorageSync("alert");
					if(this.$u.test.empty(alert)){
						this.show = true;
						this.selectItem = item;
						uni.setStorageSync("alert","1");
						return;
					}
					this.selectItem = item;
					this.getAdv();
				}else if(item.type == 'platform'){
					uni.navigateTo({
						url:'/pages/miniProgram/main'
					})
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
	
	.main_bg{
		background-color #eee;
	}
	
	.btn_class{
		display flex;
		margin-top 20rpx;
		margin-left 20rpx;
		margin-right 20rpx;
		height 130rpx;
		background-color #fff;
		border-radius 10rpx;
		justify-content space-between;
		align-items center;
		
		.left_classz{
			width 64rpx;
			height 64rpx;
			border-radius 32rpx;
			background-color rgba(0, 0, 0, 0);
			font-size 30rpx;
			font-weight bold;
		}
	}
	
	.item_class{
		display flex;
		margin-top 20rpx;
		margin-left 20rpx;
		margin-right 20rpx;
		height 130rpx;
		background-color #fff;
		border-radius 10rpx;
		justify-content space-between;
		align-items center;
		
		
	}
	
	.left_class{
		margin-left 33rpx;
		width 64rpx;
		height 64rpx;
		border-radius 32rpx;
		background-color rgba(0, 0, 0, 0);
		font-size 30rpx;
		font-weight bold;
	}
	
	.middle_class{
		display flex;
		flex-direction column;
		margin-left 20rpx;
		text-align left;
		flex 1;
		
		.middle_top {
			font-size 28rpx;
			font-weight bold;
			line-height 40rpx;
			height 40rpx;
		}
		
		.middle_bottom {
			color #999999;
			font-size 22rpx;
			line-height 30rpx;
			height 30rpx;
		}
	}
	
	.adunit_class {
		position fixed;
		width 100%;
		bottom 0rpx;
	}
	
</style>
